// pages/details/details.js
const app = getApp();
const douban = app.globalData.doubanUrl;
import utils from "../../utils/utils";
const http = utils.http;
var star = utils.star;
var handleCasts = utils.handleCasts;
var handleGenres = utils.handleGenres;
Page({
  data: {
    isEmpty: true
  },
  onLoad: function (options) {
    wx.showLoading({
      title: "数据加载中"
    });
    var id = options.id;
    console.log(options.id);  //输出了该的id
    var url = douban + "subject/" + options.id;//加上之后，便是那个电影的接口
    http(url, this.handleData)
  },
  handleData(res) {  //取数据
    var data = res.data;
    var title = data.title;
    var country = data.countries[0] + "." + data.year;
    var like = data.wish_count;
    var comment = data.comment_count;
    var banner = data.images.large;
    var alt_title = data.original_title;
    var director = data.directors[0].name;
    var stars = star(data.rating.stars);
    var average = data.rating.average;
    var castsName = handleCasts(data.casts);
    var genres = handleGenres(data.genres);
    var sum = data.summary;
    var casts = data.casts;
    this.setData({  //将刚刚取出来的数据，放进去！
      title, country, like, comment, banner, alt_title, stars, average, director, castsName, genres, sum, casts
    })
    wx.hideLoading();  //数据渲染完后，将loading取消
  },
  onPreview() {  //预览，将小图双击放大
    var self = this;
    wx.previewImage({
      urls: [self.data.banner]
    })
  },
  click() {  //用于换头像
    wx.chooseImage({
      count: 9,	// 默认为9
      sizeType: ['original', 'compressed'],	// 指定原图或者压缩图
      sourceType: ['album', 'camera'],	// 指定图片来源
      success: res => {
        var tempFilePaths = res.tempFilePaths;
        this.setData({
          imgUrl: tempFilePaths,
          isEmpty: false
        })
      }
    })
  }
})